<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Factor;
use app\common\model\Fenorder;
use think\Db;
use app\common\model\PrepareQueue as PrepareQueueModel;

/**
 * 技师房控制器
 * @package app\index\controller
 */
class EngineerRoom extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取钟房所有用户数据
     */
    public function index(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $where['oq.store_id'] = $userInfo['store_id'];
            //性别筛选
            if(!empty($getData['sex'])){
                $where['u.sex'] = $getData['sex'];
            }
            //班次筛选
            if(!empty($getData['work_type'])){
                $where['u.work_type'] = $getData['work_type'];
            }
            //状态筛选
            if(!empty($getData['work_status'])){
                $where['u.work_status'] = $getData['work_status'];
            }
            //技师工号搜索
            if(!empty($getData['work_number'])){
                $where['u.work_number'] = $getData['work_number'];
            }
            //待安排
            if(!empty($getData['room'])){
                if($getData['room'] == '2'){
                    $where['u.room_id'] = '0';
                }
            }
            //用户数据
            $userRes = Db::name('qt_order_queue')
                ->field('oq.id,u.id as user_id,u.work_number,u.sex,wt.title as name,oq.room_id,oq.sort,u.work_type,u.work_status,st.title as work_title,oq.head,oq.foot,oq.start_time,oq.end_time,u.top_type_id')
                ->alias('oq')
                ->leftJoin('qt_users u','u.id = oq.user_id')
                ->leftJoin('qt_work_type wt','wt.id = u.top_type_id')
                ->leftJoin('qt_scheduling_type st','st.id = u.work_type')
                ->where($where)
                ->order('oq.sort asc')
                ->paginate($getData['limit'])->each(function ($item){
                    $item['clock'] = Db::name('qt_commission_log')
                        ->where(['user_id'=>$item['user_id'],'type'=>'1','status'=>'2'])
                        ->whereTime('created_time','today')
                        ->count();
                    $item['row'] = Db::name('qt_commission_log')
                        ->where(['user_id'=>$item['user_id'],'type'=>'1','status'=>'1'])
                        ->whereTime('created_time','today')
                        ->count();
                    $item['room'] = Db::name('qt_room')
                        ->where(['id'=>$item['room_id']])
                        ->value('name');
                    return $item;

                });
            //总数据
            $this->result('查询成功', $userRes, 200);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $list =  Db::name('qt_order_queue')->field('id,sort')
                ->where('sort','neq','0')
                ->where('store_id',$userInfo['store_id'])
                ->order('sort asc')
                ->select();
            //获取老用户的坐标位置
            $oldSort  = Db::name('qt_order_queue')->where('id',$getData['old_sort'])->value('sort');
            $oldNumber = Db::name('qt_order_queue')->where('sort','<',$oldSort)->count();
            //获取新用户的坐标位置
            $newSort  = Db::name('qt_order_queue')->where('id',$getData['new_sort'])->value('sort');
            $newNumber = Db::name('qt_order_queue')->where('sort','<',$newSort)->count();
            $oldSort = $oldNumber;
            if($getData['type'] == '1'){
                //之前
                $newSort = $newNumber + 1;
            }else{
                $newSort = $newNumber;
            }
            //id 主键 sort 排序值
            //id集合
            $idArr   = [];
            //排序集合
            $sortArr = [];
            foreach ($list as $item) {
                $idArr[]   = $item['id'];
                $sortArr[] = $item['sort'];
            }
            //记录要拖动的id
            $oldValue = $idArr[$oldSort];
            //删除这个要拖动的id
            unset($idArr[$oldSort]);
            //插入新的位置，并自动移位
            array_splice($idArr, $newSort, 0, $oldValue);
            //重新设置排序
            $set = [];
            for ($i = 0; $i < count($idArr); $i++) {
                $set[$i]['id']   = $idArr[$i];
                $set[$i]['sort'] = $sortArr[$i];
            }
            foreach ($set as $key=>$value){
                Db::name('qt_order_queue')->where('id',$value['id'])->update(['sort'=>$value['sort']]);
            }
            //保存到数据库
            $this->result('排序成功', '', 200);
        }

    }

    /**
     * 头牌或者尾排
     */

    public function topOrTail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $list = Db::name('qt_order_queue')->field('id,sort')
                ->where('sort', 'neq', '0')
                ->where('store_id', $userInfo['store_id'])
                ->order('sort asc')
                ->select();
            //获取老用户的坐标位置
            $oldSort = Db::name('qt_order_queue')->where('user_id', $getData['user_id'])->value('sort');
            $oldNumber = Db::name('qt_order_queue')
                ->where(['store_id'=>$this->userInfo['store_id']])
                ->where('sort', '<', $oldSort)
                ->count();
            //头牌
            if($getData['type'] == '1'){
                $newNumber = '0';
            }
            //尾牌
            if($getData['type'] == '2'){
                $newNumber = Db::name('qt_order_queue')
                    ->where('sort', 'neq', '0')
                    ->where(['store_id'=>$this->userInfo['store_id']])
                    ->count();
            }
            $oldSort = $oldNumber;
            $newSort = $newNumber;
            //id 主键 sort 排序值
            //id集合
            $idArr = [];
            //排序集合
            $sortArr = [];
            foreach ($list as $item) {
                $idArr[] = $item['id'];
                $sortArr[] = $item['sort'];
            }
            //记录要拖动的id
            $oldValue = $idArr[$oldSort];
            //删除这个要拖动的id
            unset($idArr[$oldSort]);
            //插入新的位置，并自动移位
            array_splice($idArr, $newSort, 0, $oldValue);
            //重新设置排序
            $set = [];
            for ($i = 0; $i < count($idArr); $i++) {
                $set[$i]['id'] = $idArr[$i];
                $set[$i]['sort'] = $sortArr[$i];
            }
            foreach ($set as $key => $value) {
                Db::name('qt_order_queue')->where('id', $value['id'])->update(['sort' => $value['sort']]);
            }
            //保存到数据库
            $this->result('操作成功', '', 200);
        }
    }


    /**
     * 获取技师前一位和后一位的排序
     */
    public function frontOrAfter(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            $engineer_id = Db::name('qt_users')
                ->where(['work_number'=>$getData['work_number'],'store_id'=>$this->userInfo['store_id']])
                ->value('id');
            $sort = Db::name('qt_order_queue')->where('user_id',$engineer_id)->find();
            $frontSort = Db::name('qt_order_queue')
                ->where('store_id',$this->userInfo['store_id'])
                ->where('sort','<',$sort['sort'])
                ->order('sort desc')
                ->limit('1')
                ->value('id');
            if(empty($frontSort)){
                $frontSort = '';
            }
            $afterSort = Db::name('qt_order_queue')
                ->where('store_id',$this->userInfo['store_id'])
                ->where('sort','>',$sort['sort'])
                ->order('sort asc')
                ->limit('1')
                ->value('id');
            if(empty($afterSort)){
                $afterSort = '';
            }
            $result['sort'] = $sort['id'];
            $result['front_sort'] = $frontSort;
            $result['after_sort'] = $afterSort;
            $this->result('查询成功', $result, 200);
        }
    }

}
